package messages;

import java.sql.ResultSet;

import javax.swing.JOptionPane;

import controllers.DBConnection;
import controllers.SendMail;
import ocsf.server.ConnectionToClient;

public class RequestUpdatePricesMessage {

	
	ConnectionToClient client;
	String[] arr;
	double sample,picture,layer,time,pac;
	/**
	 * getting for the worker the Request Update Prices Message 
	 * @param strMsg
	 * @param client
	 */
	
	
	public RequestUpdatePricesMessage(String[] strMsg, ConnectionToClient client){
		this.arr=strMsg;
		this.client=client;
		
		/**
		 * get new prices and convert from String to Double
		 */
		this.sample=Double.parseDouble(arr[1]);
		this.picture=Double.parseDouble(arr[2]);
		this.layer=Double.parseDouble(arr[3]);
		this.time=Double.parseDouble(arr[4]);
		this.pac=Double.parseDouble(arr[5]);
	}
	
	
	
	public void activateRequest(){
		
		/**
		 * UPDATE request to DB
		 */
	try{	
		
		DBConnection conn=new DBConnection();
		
		if(!arr[1].equals("")){
			java.sql.PreparedStatement pr1 = conn.getDBConnection().prepareStatement( "Update sgi_system.prices SET newPrice=? WHERE ID=? ");
            pr1.setDouble(1,sample);
            pr1.setString(2,"101");
			pr1.executeUpdate();	
			pr1.close();
		}
		if(!arr[2].equals("")){
			java.sql.PreparedStatement pr2 = conn.getDBConnection().prepareStatement( "Update sgi_system.prices SET newPrice=? WHERE ID=? ");
            pr2.setDouble(1,picture);
            pr2.setString(2,"102");
			pr2.executeUpdate();	
			pr2.close();
		}	
		if(!arr[3].equals("")){
			java.sql.PreparedStatement pr3 = conn.getDBConnection().prepareStatement( "Update sgi_system.prices SET newPrice=? WHERE ID=? ");
            pr3.setDouble(1,layer);
            pr3.setString(2,"103");
			pr3.executeUpdate();	
			pr3.close();
		}		
		if(!arr[4].equals("")){
			java.sql.PreparedStatement pr4 = conn.getDBConnection().prepareStatement( "Update sgi_system.prices SET newPrice=? WHERE ID=? ");
            pr4.setDouble(1,time);
            pr4.setString(2,"104");
			pr4.executeUpdate();	
			pr4.close();
		}		
		if(!arr[5].equals("")){
			java.sql.PreparedStatement pr5 = conn.getDBConnection().prepareStatement( "Update sgi_system.prices SET newPrice=? WHERE ID=? ");
            pr5.setDouble(1,pac);
            pr5.setString(2,"105");
			pr5.executeUpdate();	
			pr5.close();
		}		
			
		System.out.println("RequestUpdatePrices is done");
		String address="igal.f202@gmail.com";
		String messageTo="new prices approved and updated in DB";
		SendMail mail=new SendMail(address,messageTo);
		//client.sendToClient("RequestUpdatePrices");
			
	}catch(Exception e){
		e.printStackTrace();
	}
	

	}
}

